package com.mrzhou.study.struct.support;

/**
 * 算术运算的基本操作
 */
public class CalculateUtil {

    private static final char[] chars = {'+', '-', '*', '/'};

    public static int choose(int var1, int var2, char operator){
        int i = 0;
        switch (operator) {
            case '+' :
                i = add(var1, var2);
                break;
            case '-' :
                i = subtract(var1, var2);
                break;
            case '*' :
                i = multiply(var1, var2);
                break;
            case '/' :
                i = divide(var1, var2);
                break;
            default:
                break;
        }
        return i;
    }

    /**
     * 加法运算
     */
    public static int add(int var1, int var2) {
        return var1 + var2;
    }

    /**
     * 减法运算
     */
    public static int subtract(int var1, int var2) {
        return var1 - var2;
    }

    /**
     * 乘法运算
     */
    public static int multiply(int var1, int var2) {
        return var1 * var2;
    }

    /**
     * 除法运算
     */
    public static int divide(int var1, int var2) {
        return var1 / var2;
    }

    private static int charAt(char c) {
        for(int i = 0; i < chars.length; i++) {
            if(c == chars[i]) {
                return i;
            }
        }
        return -1;
    }

    /**
     * 判断是否是数字
     */
    public static boolean isDigit(char c) {
        return c >= '0' && c <= '9' ;
    }

    /**
     * 运算符优先级计算
     * @param oper
     * @return
     */
    public static int priority(char oper) {
        if(oper == '*' || oper == '/') {
            return 1;
        }
        if(oper == '+' || oper == '-') {
            return 0;
        }
        return -1;
    }

    /**
     * 判断字符是否为合法的字符串
     */
    public static boolean isLegalOper(char c) {
        return c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')';
    }

    /**
     * 判断字符是否为算式的运算符
     */
    public static boolean isOper(char c) {
        return c == '+' || c == '-' || c == '*' || c == '/';
    }

    public static boolean isOpers(char... chars) {
        boolean result = true;
        for (char c : chars) {
            if (!isOper(c)) {
                result = false;
                break;
            }
        }
        return result;
    }
}
